/*
 * VentBalanceComprobacion.java
 *
 * Created on 3 de septiembre de 2008, 10:50 AM
 */

package administrador;

import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author  daniel1
 */
public class VentBalanceComprobacion extends javax.swing.JPanel {
Conexion conexion;
Usuario user;
AdministradorView aview;
List vMayor;

//Nath Redondeo
NumberFormat nf = NumberFormat.getNumberInstance();
   
    /** Creates new form VentBalanceComprobacion */
    public VentBalanceComprobacion(Conexion cEnt,Usuario user,AdministradorView aview,List vectMayor) {
        DecimalFormat dec=(DecimalFormat)nf;
        dec.applyPattern("###.##");
         //Configuro Redondeo
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.HALF_UP);
      
        
        vMayor=vectMayor;
        this.aview=aview;
        conexion=cEnt;
        this.user=user;
        initComponents();
        cargoDatos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_nombre = new javax.swing.JLabel();
        lbl_ruc = new javax.swing.JLabel();
        lbl_titulo = new javax.swing.JLabel();
        lbl_fecha = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        txt_totalDeudor = new javax.swing.JTextField();
        txt_totalAcreedor = new javax.swing.JTextField();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(VentBalanceComprobacion.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N
        setLayout(null);

        lbl_nombre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_nombre.setText(resourceMap.getString("lbl_nombre.text")); // NOI18N
        lbl_nombre.setName("lbl_nombre"); // NOI18N
        add(lbl_nombre);
        lbl_nombre.setBounds(290, 20, 107, 14);

        lbl_ruc.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_ruc.setText(resourceMap.getString("lbl_ruc.text")); // NOI18N
        lbl_ruc.setName("lbl_ruc"); // NOI18N
        add(lbl_ruc);
        lbl_ruc.setBounds(290, 40, 109, 14);

        lbl_titulo.setText(resourceMap.getString("lbl_titulo.text")); // NOI18N
        lbl_titulo.setName("lbl_titulo"); // NOI18N
        add(lbl_titulo);
        lbl_titulo.setBounds(240, 60, 205, 14);

        lbl_fecha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_fecha.setText(resourceMap.getString("lbl_fecha.text")); // NOI18N
        lbl_fecha.setName("lbl_fecha"); // NOI18N
        add(lbl_fecha);
        lbl_fecha.setBounds(240, 80, 210, 14);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Codigo", "Nombre de la Cuenta", "Deudor", "Acreedor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setName("tabla"); // NOI18N
        jScrollPane1.setViewportView(tabla);

        add(jScrollPane1);
        jScrollPane1.setBounds(50, 100, 610, 402);

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(300, 510, 40, 20);

        txt_totalDeudor.setText(resourceMap.getString("txt_totalDeudor.text")); // NOI18N
        txt_totalDeudor.setName("txt_totalDeudor"); // NOI18N
        add(txt_totalDeudor);
        txt_totalDeudor.setBounds(350, 510, 140, 20);

        txt_totalAcreedor.setText(resourceMap.getString("txt_totalAcreedor.text")); // NOI18N
        txt_totalAcreedor.setName("txt_totalAcreedor"); // NOI18N
        add(txt_totalAcreedor);
        txt_totalAcreedor.setBounds(500, 510, 140, 20);
    }// </editor-fold>//GEN-END:initComponents

    public void cargoDatos() {
//Cargo los Datos de la Cabecera del Balance de Comprobacion:
        ResultSet resp = conexion.consultarBase("SELECT * FROM parametros p");
        try {
            resp.beforeFirst();
            resp.next();
            //Nombre de la Empresa:
            lbl_nombre.setText(resp.getString(2));

            //Ruc de la Empresa:
            lbl_ruc.setText("RUC: "+resp.getString(3));
            
            //Fecha
            Date now = new Date();
            lbl_fecha.setText(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(now));
            
            
        } catch (SQLException ex) {
            Logger.getLogger(VentBalanceComprobacion.class.getName()).log(Level.SEVERE, null, ex);
        }

        llenoCuentas();
    }

public void llenoCuentas(){
 int tamano=vMayor.size();
    
    for(int i=0;i<tamano;i++){
    String codigo=((List)vMayor.get(i)).get(0).toString();
    String nombre=((List)vMayor.get(i)).get(1).toString();
    double saldo=Double.parseDouble(((List)vMayor.get(i)).get(2).toString());
    
    //System.out.println("Las cuentas son:"+codigo+" nombre:"+nombre+" saldo"+saldo);    
    //Saco la fila desocupada:
    int desocupada=cuentaFilaOcupadas(1);
    tabla.setValueAt(codigo, desocupada,0);
    tabla.setValueAt(nombre, desocupada,1);
    
    if(saldo<0){
    saldo=saldo*-1;
    tabla.setValueAt(saldo, desocupada,3);
    }else{
    tabla.setValueAt(saldo, desocupada,2);
    }
    
    
    }
    
    //Actualizo Totales:
    actualizoTotales();
}

public void actualizoTotales(){
////System.out.println("Entra en actualiza");
        int numeroCol=tabla.getRowCount();
        int cont=0;
        double acumula=0;
        String toma;
    
    //Suma Deudor:
     double sumaDeudor=0;
     //Muestro Los Subtotales:
        while (cont<numeroCol){
            
            try{
                toma=tabla.getValueAt(cont, 2).toString();
                acumula=acumula+Double.parseDouble(toma);
                sumaDeudor=acumula;
            } catch(Exception e){};
            cont++;
        }
        txt_totalDeudor.setText(String.valueOf(nf.format(sumaDeudor).replaceAll(",",".")));
        
//Suma Acreedor
        cont=0;
        acumula=0;
        
      double sumaAcreedor=0;  
     //Muestro Los Subtotales:
        while (cont<numeroCol){
            
            try{
                toma=tabla.getValueAt(cont, 3).toString();
                acumula=acumula+Double.parseDouble(toma);
                sumaAcreedor=acumula;
            } catch(Exception e){};
            cont++;
        }
        txt_totalAcreedor.setText(String.valueOf(nf.format(sumaAcreedor).replaceAll(",",".")));
        


}


public int cuentaFilaOcupadas(int columna){
       int totalfilas=tabla.getRowCount();
       int contador=0;
       Object valor;
       for (contador=0;contador<totalfilas;contador++)
       {
           valor=tabla.getValueAt(contador,columna);
           if (valor==null){
            break;
           }
           
       }
       //System.out.println("El numero de fila ocupadas es:"+contador);
        return contador;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_fecha;
    private javax.swing.JLabel lbl_nombre;
    private javax.swing.JLabel lbl_ruc;
    private javax.swing.JLabel lbl_titulo;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txt_totalAcreedor;
    private javax.swing.JTextField txt_totalDeudor;
    // End of variables declaration//GEN-END:variables

}
